<template>
  <div class="home-banner">
    <!-- 主轮播图 -->
    <div class="main-carousel">
      <div class="carousel-container">
        <div class="carousel-wrapper" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
          <div 
            v-for="banner in banners" 
            :key="banner.id"
            class="banner-slide" 
            :style="{ backgroundImage: `url(${banner.image})` }"
          >
            <div class="banner-content">
              <h2 class="banner-title">{{ banner.title }}</h2>
              <p class="banner-desc">{{ banner.description }}</p>
            </div>
          </div>
        </div>
        
        <!-- 左右切换按钮 -->
        <button class="carousel-control prev" @click="prev">
          <i class="fas fa-chevron-left"></i>
        </button>
        <button class="carousel-control next" @click="next">
          <i class="fas fa-chevron-right"></i>
        </button>
        
        <!-- 指示器 -->
        <div class="carousel-indicators">
          <span 
            v-for="(banner, index) in banners" 
            :key="index"
            class="indicator"
            :class="{ active: index === currentIndex }"
            @click="goTo(index)"
          ></span>
        </div>
      </div>
    </div>
    
    <!-- 小型数据统计 -->
    <div class="mini-stats">
      <div class="stat-item">
        <span class="stat-number">12,856.8</span>
        <span class="stat-label">累计交易额(万元)</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">15,680</span>
        <span class="stat-label">注册用户</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">245</span>
        <span class="stat-label">合作基地</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">1,280</span>
        <span class="stat-label">合作农户</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">89</span>
        <span class="stat-label">合作工厂</span>
      </div>
    </div>
  </div>
</template>

<script setup>
const currentIndex = ref(0)
let autoPlayTimer = null

const banners = [
  {
    id: 1,
    image: '/images/banner-1.jpg',
    title: '优质菌种供应',
    description: ''
  },
  {
    id: 2,
    image: '/images/banner-2.jpg',
    title: '全国基地直供',
    description: '优质货源 · 产地直发'
  },
  {
    id: 3,
    image: '/images/banner-3.jpg',
    title: '品质保障 诚信交易',
    description: '安全可靠的交易平台'
  }
]


const prev = () => {
  currentIndex.value = currentIndex.value === 0 ? banners.length - 1 : currentIndex.value - 1
}

const next = () => {
  currentIndex.value = currentIndex.value === banners.length - 1 ? 0 : currentIndex.value + 1
}

const goTo = (index) => {
  currentIndex.value = index
}

const startAutoPlay = () => {
  autoPlayTimer = setInterval(() => {
    next()
  }, 5000)
}

const stopAutoPlay = () => {
  if (autoPlayTimer) {
    clearInterval(autoPlayTimer)
    autoPlayTimer = null
  }
}

onMounted(() => {
  startAutoPlay()
})

onUnmounted(() => {
  stopAutoPlay()
})
</script>

<style lang="scss" scoped>
.home-banner {
  display: flex;
  flex-direction: column;
  gap: 10px;
  height: 530px;
  
  .main-carousel {
    flex: 1;
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    
    .carousel-container {
      position: relative;
      width: 100%;
      height: 100%;
      overflow: hidden;
      
      .carousel-wrapper {
        display: flex;
        height: 100%;
        transition: transform 0.5s ease-in-out;
      }
      
      .carousel-control {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 40px;
        height: 40px;
        background: rgba(0, 0, 0, 0.3);
        border: none;
        border-radius: 50%;
        color: #fff;
        font-size: 16px;
        cursor: pointer;
        transition: all 0.3s;
        z-index: 10;
        
        &:hover {
          background: rgba(228, 57, 60, 0.8);
          transform: translateY(-50%) scale(1.1);
        }
        
        &.prev {
          left: 20px;
        }
        
        &.next {
          right: 20px;
        }
      }
      
      .carousel-indicators {
        position: absolute;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        display: flex;
        gap: 10px;
        z-index: 10;
        
        .indicator {
          width: 30px;
          height: 3px;
          background: rgba(255, 255, 255, 0.5);
          border-radius: 2px;
          cursor: pointer;
          transition: all 0.3s;
          
          &:hover {
            background: rgba(255, 255, 255, 0.8);
          }
          
          &.active {
            background: #fff;
          }
        }
      }
    }
    
    .banner-slide {
      min-width: 100%;
      height: 100%;
      background-size: cover;
      background-position: center;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        inset: 0;
        background: linear-gradient(to bottom, rgba(0,0,0,0.2), rgba(0,0,0,0.4));
      }
      
      .banner-content {
        position: absolute;
        bottom: 60px;
        left: 60px;
        z-index: 1;
        color: #fff;
        text-shadow: 0 3px 10px rgba(0, 0, 0, 0.6);
        
        .banner-title {
          margin: 0 0 15px;
          font-size: 36px;
          font-weight: 800;
          letter-spacing: 2px;
          animation: slideInLeft 0.8s ease;
        }
        
        .banner-desc {
          margin: 0;
          font-size: 20px;
          opacity: 0.95;
          letter-spacing: 1px;
          animation: slideInLeft 1s ease;
        }
      }
      
      @keyframes slideInLeft {
        from {
          opacity: 0;
          transform: translateX(-30px);
        }
        to {
          opacity: 1;
          transform: translateX(0);
        }
      }
    }
  }
  
  .quick-entries {
    display: flex;
    gap: 10px;
    height: 240px;
    
    .entry-item {
      flex: 1;
      position: relative;
      border-radius: 6px;
      overflow: hidden;
      text-decoration: none;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
        
        img {
          transform: scale(1.05);
        }
        
        .entry-overlay {
          background: rgba(228, 57, 60, 0.9);
        }
      }
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: all 0.3s;
      }
      
      .entry-overlay {
        position: absolute;
        inset: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0.4);
        transition: all 0.3s;
        
        span {
          color: #fff;
          font-size: 18px;
          font-weight: 600;
          text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
      }
    }
  }
  
  // 小型数据统计样式
  .mini-stats {
    display: flex;
    justify-content: space-between;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 6px;
    padding: 12px 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    
    .stat-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      text-align: center;
      flex: 1;
      
      .stat-number {
        font-size: 18px;
        font-weight: 700;
        color: #e4393c;
        line-height: 1.2;
        margin-bottom: 4px;
      }
      
      .stat-label {
        font-size: 11px;
        color: #666;
        line-height: 1.2;
        white-space: nowrap;
      }
    }
    
    @media (max-width: 768px) {
      padding: 10px 15px;
      
      .stat-item {
        .stat-number {
          font-size: 16px;
        }
        
        .stat-label {
          font-size: 10px;
        }
      }
    }
    
    @media (max-width: 480px) {
      flex-wrap: wrap;
      gap: 8px;
      
      .stat-item {
        flex: 0 0 calc(33.333% - 6px);
        
        .stat-number {
          font-size: 14px;
        }
        
        .stat-label {
          font-size: 9px;
        }
      }
    }
  }
}
</style>
